package com.cx.purchase.legalplant.controller;

import com.cx.purchase.config.ResultVO;
import com.cx.purchase.config.ResultVOBuilder;

import com.cx.purchase.legalplant.model.*;
import com.cx.purchase.legalplant.service.LegalPlantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@CrossOrigin
public class LegalPlantContorller {

    @Autowired
    private LegalPlantService legalPlantService;

    @PostMapping("legalPlant/query")
    public ResultVO ofCurrentUser(@RequestBody LegalPlantSelectVo lgalPlantSelectVo){
        return legalPlantService.queryPage(lgalPlantSelectVo);
    }
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVO queryLegalPerson(){
        Integer id = 1;
        if (id==null){
            return ResultVOBuilder.error("用户ID不能为空！");
        }
        return legalPlantService.queryLegalPerson(id);
    }

    @PostMapping("legalPlant/add")
    public ResultVO add(@RequestBody @Valid LegalPlantAddVO legalPlantAddVO, BindingResult bd){
        System.out.println(legalPlantAddVO);
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        return legalPlantService.add(legalPlantAddVO);
    }

    @PostMapping("legalPlant/update")
    public ResultVO update(@RequestBody @Valid LegalPlantUpdateVO legalPlantUpdateVO, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        return legalPlantService.update(legalPlantUpdateVO);
    }

    @PostMapping("legalPlant/updatestatus")
    public ResultVO updatestatus(@RequestBody @Valid LegalPlantUpdateStatus legalPlantUpdateStatus, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        return legalPlantService.updateStatus(legalPlantUpdateStatus);
    }

}
